﻿using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Bing.Test
{
    /// <summary>
    /// Summary description for ApiTest
    /// </summary>
    [TestClass]
    public class ApiTest
    {
        private const string API_KEY = "<Your AppId Here>"; // TODO: Add your AppId
        private const string BAD_API_KEY = "My Bad Key";
        private const uint PROPERTY_ID = 0; // TODO: Add your Property Id
        private const uint AD_UNIT_ID = 0; // TODO: Add your AdUnit Id

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext { get; set; }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        /// <summary>
        /// Tests the with bad API key.
        /// </summary>
        [TestMethod]
        public void TestWithBadApiKey()
        {
            SearchRequest searchRequest = new SearchRequest {AppId = BAD_API_KEY, Query = "speling", Market = "en-US"};

            SpellResponse spellResponse = API.Spell(searchRequest, new SpellRequest());
            Assert.IsTrue(spellResponse.Errors.Count > 0, "There were no errors reported!" );
        }

        /// <summary>
        /// Test the Api.Ad method
        /// </summary>
        [TestMethod]
        public void Ad()
        {
            SearchRequest searchRequest = new SearchRequest { AppId = API_KEY, Query = "Zune", Market = "en-US" };

            AdRequest adRequest = new AdRequest();
            adRequest.PropertyId = PROPERTY_ID;
            adRequest.AdUnitId = AD_UNIT_ID;
            adRequest.PageNumber = 0;

            AdResponse adResponse = API.Ad(searchRequest, adRequest);

            Assert.IsTrue(adResponse.Errors.Count == 0, "There were unexpected API errors.");
            Assert.IsTrue(adResponse.Total > 0, "The total was zero");
            Assert.IsTrue(adResponse.Results.Count > 0, "There were no results.");

        }

        /// <summary>
        /// Test the Api.Image method
        /// </summary>
        [TestMethod]
        public void Image()
        {
            SearchRequest searchRequest = new SearchRequest { AppId = API_KEY, Query = "xbox site:microsoft.com", Market = "en-US" };

            ImageRequest imageRequest = new ImageRequest();
            imageRequest.Filters = new[] { "Size:Medium", "Color:Color" };
            imageRequest.Count = 10;  // Required by the search

            ImageResponse imageResponse = API.Image(searchRequest, imageRequest);

            Assert.IsTrue(imageResponse.Errors.Count == 0, "There were unexpected API errors.");
            Assert.IsTrue(imageResponse.Total > 0, "The total was zero");
            Assert.IsTrue(imageResponse.Results.Count > 0, "There were no results.");
        }

        /// <summary>
        /// ITest the Api.Image method without passing a count property
        /// </summary>
        [TestMethod]
        public void ImageWithoutCountProperty()
        {
            SearchRequest searchRequest = new SearchRequest { AppId = API_KEY, Query = "xbox site:microsoft.com", Market = "en-US" };

            ImageRequest imageRequest = new ImageRequest();
            imageRequest.Filters = new[] { "Size:Medium", "Color:Color" };

            ImageResponse imageResponse = API.Image(searchRequest, imageRequest);

            Assert.IsTrue(imageResponse.Errors.Count > 0, "There were no API errors.");
        }

        /// <summary>
        /// Test the Api.Image method with two size filters, which is not allowed
        /// </summary>
        [TestMethod]
        public void ImageWithoutTwoSizeFilters()
        {
            SearchRequest searchRequest = new SearchRequest { AppId = API_KEY, Query = "xbox site:microsoft.com", Market = "en-US" };

            ImageRequest imageRequest = new ImageRequest();
            imageRequest.Filters = new[] { "Size:Medium", "Size:Small" };
            imageRequest.Count = 10;

            ImageResponse imageResponse = API.Image(searchRequest, imageRequest);

            Assert.IsTrue(imageResponse.Errors.Count == 0, "There were unexpected API errors.");
            Assert.IsTrue(imageResponse.Results.Count==0, "There were unexpected results returned.");
        }

        /// <summary>
        /// Test the Api.InstantAnswer method with an Encarta define query.
        /// </summary>
        [TestMethod]
        public void InstantAnswerEncartaDefine()
        {
            SearchRequest searchRequest = new SearchRequest { AppId = API_KEY, Query = "Define salient", Market = "en-US" };

            InstantAnswerRequest instantAnswerRequest= new InstantAnswerRequest();
            InstantAnswerResponse instantAnswerResponse = API.InstantAnswer(searchRequest, instantAnswerRequest);

            Assert.IsTrue(instantAnswerResponse.Errors.Count == 0, "There were unexpected API errors.");
            Assert.IsTrue(instantAnswerResponse.Results.Count > 0, "There were no results returned.");
            Assert.IsNotNull(instantAnswerResponse.Results[0].Encarta);
        }

        /// <summary>
        /// Test the Api.InstantAnswer method with an Encarta calculation query.
        /// </summary>
        [TestMethod]
        public void InstantAnswerEncartaCalculate()
        {
            SearchRequest searchRequest = new SearchRequest { AppId = API_KEY, Query = "2*2", Market = "en-US" };

            InstantAnswerRequest instantAnswerRequest = new InstantAnswerRequest();
            InstantAnswerResponse instantAnswerResponse = API.InstantAnswer(searchRequest, instantAnswerRequest);

            Assert.IsTrue(instantAnswerResponse.Errors.Count == 0, "There were unexpected API errors.");
            Assert.IsTrue(instantAnswerResponse.Results.Count > 0, "There were no results returned.");
            Assert.IsNotNull(instantAnswerResponse.Results[0].Encarta);
            int value = int.Parse(instantAnswerResponse.Results[0].Encarta.Value);
            Assert.IsTrue(value == 2*2);
        }

        /// <summary>
        /// Test the Api.InstantAnswer method with an Encarta population query.
        /// </summary>
        [TestMethod]
        public void InstantAnswerEncartaPopulation()
        {
            SearchRequest searchRequest = new SearchRequest { AppId = API_KEY, Query = "population of united states", Market = "en-US" };

            InstantAnswerRequest instantAnswerRequest = new InstantAnswerRequest();
            InstantAnswerResponse instantAnswerResponse = API.InstantAnswer(searchRequest, instantAnswerRequest);

            Assert.IsTrue(instantAnswerResponse.Errors.Count == 0, "There were unexpected API errors.");
            Assert.IsTrue(instantAnswerResponse.Results.Count > 0, "There were no results returned.");
            Assert.IsNotNull(instantAnswerResponse.Results[0].Encarta);
        }

        /// <summary>
        /// Test the Api.InstantAnswer method with an Encarta flight status query.
        /// </summary>
        [TestMethod]
        public void InstantAnswerFlightStatus()
        {
            SearchRequest searchRequest = new SearchRequest { AppId = API_KEY, Query = "UA820", Market = "en-US" };

            InstantAnswerRequest instantAnswerRequest = new InstantAnswerRequest();
            InstantAnswerResponse instantAnswerResponse = API.InstantAnswer(searchRequest, instantAnswerRequest);

            Assert.IsTrue(instantAnswerResponse.Errors.Count == 0, "There were unexpected API errors.");
            Assert.IsTrue(instantAnswerResponse.Results.Count > 0, "There were no results returned.");
            Assert.IsNotNull(instantAnswerResponse.Results[0].FlightStatus);
        }

        /// <summary>
        /// Test the Api.MobileWeb method.
        /// </summary>
        [TestMethod]
        public void MobileWeb()
        {

            SearchRequest searchRequest = new SearchRequest { AppId = API_KEY, Query = "Zune", Market = "en-US" };

            MobileWebRequest mobileWebRequest = new MobileWebRequest();
            mobileWebRequest.Count = 10;  // Required by the search
            mobileWebRequest.Options = new[] {ApiHelper.WebSearchOption.DisableQueryAlterations};

            MobileWebResponse mobileWebResponse = API.MobileWeb(searchRequest, mobileWebRequest);

            Assert.IsTrue(mobileWebResponse.Errors.Count == 0, "There were unexpected API errors.");
            Assert.IsTrue(mobileWebResponse.Total > 0, "The total was zero");
            Assert.IsTrue(mobileWebResponse.Results.Count > 0, "There were no results.");
        }

        /// <summary>
        /// Test the Api.News method.
        /// </summary>
        [TestMethod]
        public void News()
        {
            SearchRequest searchRequest = new SearchRequest { AppId = API_KEY, Query = "obama", Market = "en-US" };

            NewsRequest newsRequest = new NewsRequest();
            //newsRequest.Category = "rt_US";

            NewsResponse newsResponse = API.News(searchRequest, newsRequest);

            Assert.IsTrue(newsResponse.Errors.Count == 0, "There were unexpected API errors.");
            Assert.IsTrue(newsResponse.Total > 0, "The total was zero");
            Assert.IsTrue(newsResponse.Results.Count > 0, "There were no results.");
        }

        /// <summary>
        /// Test the Api.Phonebook method.
        /// </summary>
        [TestMethod]
        public void Phonebook()
        {
            SearchRequest searchRequest = new SearchRequest { AppId = API_KEY, Query = "Microsoft", Market = "en-US" };

            PhonebookRequest phonebookRequest = new PhonebookRequest();
            phonebookRequest.Count = 10;
            phonebookRequest.FileType = "YP";
            phonebookRequest.SortBy = ApiHelper.PhonebookSortOption.Distance;

            PhonebookResponse phonebookResponse = API.Phonebook(searchRequest, phonebookRequest);

            Assert.IsTrue(phonebookResponse.Errors.Count == 0, "There were unexpected API errors.");
            Assert.IsTrue(phonebookResponse.Total > 0, "The total was zero");
            Assert.IsTrue(phonebookResponse.Results.Count > 0, "There were no results.");
        }

        /// <summary>
        /// Test the Api.RelatedSearch method.
        /// </summary>
        [TestMethod]
        public void RelatedSearch()
        {
            SearchRequest searchRequest = new SearchRequest { AppId = API_KEY, Query = "Microsoft", Market = "en-US" };

            RelatedSearchRequest relatedSearchRequest = new RelatedSearchRequest();

            RelatedSearchResponse relatedSearchResponse = API.RelatedSearch(searchRequest, relatedSearchRequest);

            Assert.IsTrue(relatedSearchResponse.Errors.Count == 0, "There were unexpected API errors.");
            Assert.IsTrue(relatedSearchResponse.Results.Count > 0, "There were no results.");
        }

        /// <summary>
        /// Test the Api.Spell method.
        /// </summary>
        [TestMethod]
        public void Spelling()
        {
            SearchRequest searchRequest = new SearchRequest { AppId = API_KEY, Query = "speling", Market = "en-US" }; 

            SpellResponse spellResponse = API.Spell(searchRequest, new SpellRequest());

            Assert.IsTrue(spellResponse.Errors.Count== 0, "There were unexpected API errors.");
            Assert.IsTrue(spellResponse.Total > 0, "The total was zero");
            Assert.IsTrue(spellResponse.Results.Count> 0, "There were no results.");
            Assert.IsTrue(spellResponse.Total == spellResponse.Results.Count, "The total does not match the results count.");
        }

        /// <summary>
        /// Test the Api.Translation method.
        /// </summary>
        [TestMethod]
        public void Translation()
        {
            SearchRequest searchRequest = new SearchRequest { AppId = API_KEY, Query = "This is my text", Market = "en-US" };

            TranslationRequest translationRequest = new TranslationRequest();
            translationRequest.SourceLanguage = "en";
            translationRequest.TargetLanguage = "es";

            TranslationResponse translationResponse = API.Translation(searchRequest, translationRequest);

            Assert.IsTrue(translationResponse.Errors.Count == 0, "There were unexpected API errors.");
            Assert.IsTrue(translationResponse.TranslationResults.Count > 0, "There were no results.");

        }

        /// <summary>
        /// Test the Api.Web method.
        /// </summary>
        [TestMethod]
        public void Web()
        {
            SearchRequest searchRequest = new SearchRequest { AppId = API_KEY, Query = "Bing", Market = "en-US" };

            WebRequest webRequest = new WebRequest();
            webRequest.FileType = "DOC";
            webRequest.Count = 10;  // Required by the search

            WebResponse webResponse = API.Web(searchRequest, webRequest);

            Assert.IsTrue(webResponse.Errors.Count == 0, "There were unexpected API errors.");
            Assert.IsTrue(webResponse.Total > 0, "The total was zero");
            Assert.IsTrue(webResponse.Results.Count > 0, "There were no results.");
        }

        /// <summary>
        /// Test the Api.Video method.
        /// </summary>
        [TestMethod]
        public void Video()
        {
            SearchRequest searchRequest = new SearchRequest { AppId = API_KEY, Query = "xbox site:microsoft.com", Market = "en-US" };

            VideoRequest videoRequest = new VideoRequest();
            videoRequest.Filters = new[] {"Resolution:Medium" };
            videoRequest.Count = 10;  // Required by the search

            VideoResponse videoResponse = API.Video(searchRequest, videoRequest);

            Assert.IsTrue(videoResponse.Errors.Count == 0, "There were unexpected API errors.");
            Assert.IsTrue(videoResponse.Total > 0, "The total was zero");
            Assert.IsTrue(videoResponse.Results.Count > 0, "There were no results.");
        }

        /// <summary>
        /// Test the Api.Multiple method.
        /// </summary>
        [TestMethod]
        public void MultipleSources()
        {
            SearchRequest searchRequest = new SearchRequest
                                              {
                                                  AppId = API_KEY,
                                                  Query = "microsift",
                                                  Market = "en-us",
                                                  Sources = new[] {ApiHelper.SourceType.Web, ApiHelper.SourceType.Spell, ApiHelper.SourceType.Phonebook}
                                              };
            WebRequest webRequest = new WebRequest {Count = 5, FileType = "DOC"};
            PhonebookRequest phonebookRequest = new PhonebookRequest {Count = 7, FileType = "YP"};

            MultipleSourceResponse multipleSourceResponse = API.Multiple(searchRequest, webRequest, phonebookRequest);
            
            Assert.IsTrue(multipleSourceResponse.Errors.Count == 0, "There were unexpected API errors.");
            Assert.IsNotNull(multipleSourceResponse.WebResponse, "Expected a WebResponse object.");
            Assert.IsTrue(multipleSourceResponse.SpellResponse.Results.Count > 0, "Expected results from the SpellResponse object.");
            Assert.IsTrue(multipleSourceResponse.PhonebookResponse.Results.Count > 0, "Expected results from the PhonebookResponse object.");

        }

        [TestMethod]
        public void BrandAssets ()
        {
            BrandAssets assets = new BrandAssets();
            assets.Load();

            Assert.IsNotNull(assets.SearchUrl, "The search url was empty.");
            Assert.IsNotNull(assets.SearchBoxUrl, "The search box url was empty.");
            Assert.IsNotNull(assets.BrandName, "The brand name was empty.");
            Assert.IsNotNull(assets.HelpPage, "The help page was empty.");
            Assert.IsTrue(assets.ImageAssets.Count > 0, "There were no image assets returned.");
        }
    }
}